import G from '@/common/g'

// Dict 保留有用字段
function objectPick(data = {}) {
  const translate = {}
  Object.keys(data).forEach(key => {
    const value = data[key] || []
    translate[key] = {}
    value.forEach(item => {
      translate[key][item.value] = item.label
    })
  })
  return translate
}

// form 表单公共状态机

const state = {
  sysDict: {
    org_type: [],
    staff_size: [],
    stock_type: [],
    invoice_type: [],
    order_status: [],
    pay_type: [],
    source_type: [],
    confirm_status: []
  },
  sysDictTranslate: {
    org_type: {},
    staff_size: {},
    stock_type: {},
    invoice_type: {},
    order_status: {},
    pay_type: {},
    source_type: {},
    confirm_status: {}
  }
}
// getters
const getters = {
  sysDict: state => state.sysDict,
  sysDictTranslate: state => state.sysDictTranslate
}

const mutations = {
  ADD_SYSDICT: (state, data) => {
    state.sysDict = data
  },
  ADD_TRANSLATE: (state, data) => {
    state.sysDictTranslate = data
  }
}
const actions = {
  getSysDict({ commit }) {
    G.ajax({
      method: 'post',
      path: 'basisDatas/getSysDict',
      cbSuccess: function(data) {
        const res = data.memo
        commit('ADD_SYSDICT', res)
        commit('ADD_TRANSLATE', objectPick(res))
      }
    })
  }
}

export default {
  state,
  mutations,
  getters,
  actions
}
